package com.huike.web.controller.system;

import com.huike.clues.domain.SysDictData;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 数据字典数据信息接口
 * @Date 2023-10-10
 */
@RestController
@RequestMapping("/system/dict/data")
@Api(tags ="数据字典数据信息")
@Slf4j
public class SysDictDataController extends BaseController {
    @Resource
    private ISysDictDataService dictDataService;

    /**
     * 获取字典数据
     */
    @GetMapping("/type/{dictType}")
    public AjaxResult<List<SysDictData>> dictType(@PathVariable String dictType){
        List<SysDictData> data = dictDataService.selectDictDataByType(dictType);
        if(StringUtils.isNull(data)){
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }
    /**
     * 根据字典类型分页查询字典数据列表
     */
    @GetMapping(value = "/list")
    public TableDataInfo list(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTablePage(list);
    }

    /**
     * 新增字典数据
     *  @Validated 注解代表校验SysDictData对象中使用Validation的属性
     */
    @PostMapping()
    @ApiOperation("新增字典类型")
    public AjaxResult add(@Validated @RequestBody SysDictData dict){
        dict.setCreateBy(SecurityUtils.getUsername());
        dictDataService.insertDictData(dict);
        return AjaxResult.success();
    }

    /**
     * 查询字典数据详细（查询回显）
     */
    @GetMapping("/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode) {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 修改保存字典数据
     */
    @PutMapping()
    public AjaxResult update(@Validated @RequestBody SysDictData dict) {
        dict.setUpdateBy(SecurityUtils.getUsername());
        dictDataService.updateDictData(dict);
        return AjaxResult.success();
    }

    /**
     * 删除字典数据(批量删除)
     */
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return AjaxResult.success();
    }
}
